

require("Dump")
local shuffle = require("shuffle")
local PointCore = require("24Core") 

local mainFont = nil
local calculatedPoint = 0
local calculateTotal = 0
local vaildTotal = 0
local calculatePreSecond = 0
local co = nil
local poker = {
  1,1,1,1,
  2,2,2,2,
  3,3,3,3,
  4,4,4,4,
  5,5,5,5,
  6,6,6,6,
  7,7,7,7,
  8,8,8,8,
  9,9,9,9,
  10,10,10,10,
  11,11,11,11,
  12,12,12,12,
  13,13,13,13,
}

local function Combination(arr, out, num, start, picked)
  if picked == num then
    local combines = out.combines or {}
    table.insert(combines, { out[1], out[2], out[3], out[4] })
    out.combines = combines
    
    local total = out.total or 0
    total = total + 1
    out.total = total
    return
  end
    

  local _max = #arr - num + picked + 1
  for i = start, _max do
    out[picked+1] = arr[i]
    Combination(arr, out, num, i+1, picked+1)
  end  
end


function love.load()
  math.randomseed(tostring(os.time()):reverse():sub(1, 6))
  
  local out = {}
  Combination(poker, out, 4, 1, 0)  
  dp(out.total)
  calculateTotal = out.total
  co = coroutine.create(function()
      local start = os.clock()
      for k,v in ipairs(out.combines) do
--        local ret = PointCore.calculate(v)
--        if #ret > 0 then
--          vaildTotal = vaildTotal + 1
--        end
        
        local ret = PointCore.isVaild(v)
        if ret then
          vaildTotal = vaildTotal + 1
        end
                
        calculatedPoint = calculatedPoint + 1
        if os.clock() - start > 1/30 then
          coroutine.yield()
          start = os.clock()
        end
      end
      co = nil
  end)
  
  --shuffle(poker)
  --dp(poker)
  
  local start = os.clock()
  local times = 1
  for i = 1, times do
    local pickPoker = {}
    pickPoker[1] = poker[math.random(1, #poker)]
    pickPoker[2] = poker[math.random(1, #poker)]
    pickPoker[3] = poker[math.random(1, #poker)]
    pickPoker[4] = poker[math.random(1, #poker)]
    --local ret, times = PointCore.calculate(pickPoker)
  end
  print("calcu 24 point times = "..times.." cost = "..(os.clock()-start))
  
  mainFont = love.graphics.newFont("Res/visat.ttf", 30)

end

local lastCalcu = 0

local accTime = 0
local avgCalcuPreSecond = 0

function love.update(dt)
  if co then  
      assert(coroutine.resume(co))
      accTime = accTime + dt
      calculatePreSecond = calculatedPoint / accTime
  end
end

function love.draw()
  love.graphics.setFont(mainFont)
  love.graphics.print(calculatedPoint.."/"..calculateTotal, 0, 0)
  love.graphics.print(vaildTotal, 0, 40)
  love.graphics.print('cps = '..calculatePreSecond, 0 , 80)
end